package me.firstapp.manage.system;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.manage.common.BaseManage;
import me.firstapp.manage.utils.BizData4Page;
import me.firstapp.module.weixin.system.ThisSystem;
import me.firstapp.service.weixin.system.ThisSystemService;

@Controller
@RequestMapping(value = "/systemManage")
public class SystemManageAct extends BaseManage {

	@Autowired
	private ThisSystemService thisSystemService;

	@ResponseBody
	@RequestMapping(value = "/findSystemParamPage.do", method = RequestMethod.POST)
	public void findSystemParamPage(@RequestParam Map<String, Object> conditions, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<Object>();
		try {
			// 搜索条件
			Map<String, Object> searchParams = new HashMap<>();
			String sSearchJson = (String) conditions.get("sSearch");
			if (StrUtils.isNotNULL(sSearchJson)) {
				JSONObject jsonObject = JSONObject.parseObject(sSearchJson);
				if (jsonObject != null) {
					Set<String> keySet = jsonObject.keySet();
					for (String key : keySet) {
						searchParams.put(key, jsonObject.get(key));
					}
				}
			}
			// 排序字段
			String mDataProp = "mDataProp_" + conditions.get("iSortCol_0");
			mDataProp = conditions.get(mDataProp).toString().replace("Str", "");
			// 排序方式
			String sSortDir_0 = (String) conditions.get("sSortDir_0");
			// 页容
			String iDisplayLengthStr = (String) conditions.get("iDisplayLength");
			// 是开始数不是当前页数...
			String iDisplayStartStr = (String) conditions.get("iDisplayStart");
			if (StrUtils.isNotNULL(iDisplayLengthStr) && StrUtils.isNotNULL(iDisplayStartStr)) {
				Integer iDisplayLength = Integer.valueOf(iDisplayLengthStr);
				Integer iDisplayStart = Integer.valueOf(iDisplayStartStr);
				// 当前页数
				Integer pageNo = iDisplayStart / iDisplayLength + 1;
				Page<ThisSystem> thisSystemPage = thisSystemService.findThisSystemPage(pageNo, iDisplayLength,
						searchParams, mDataProp, sSortDir_0);

				BizData4Page<ThisSystem> bizData4Page = new BizData4Page<ThisSystem>();
				bizData4Page.setUserdata(conditions);
				if (thisSystemPage != null) {
					bizData4Page.setPage(thisSystemPage.getPageNo());
					bizData4Page.setPagesize(thisSystemPage.getPageSize());
					bizData4Page.setRecords(thisSystemPage.getEntityCount());
					bizData4Page.setRows(new ArrayList<ThisSystem>(thisSystemPage.getEntities()));
					bizData4Page.setTotal(thisSystemPage.getEntityCount());
				}

				result.setObject(bizData4Page);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/updateSystemParam.do", method = RequestMethod.POST)
	public void updateSystemParam(Long id, Integer orderNoLength, Long orderOutTime, BigDecimal cashFeeRate,
			BigDecimal fansWeibiToJinbiRate, BigDecimal vipWeibiToJinbiRate, BigDecimal agenterWeibiToJinbiRate,
			BigDecimal partnerWeibiToJinbiRate, Integer partnerCountsLimit, BigDecimal partnerJinbiFromAgentCashRate,
			BigDecimal spreadGiveWeiBi, BigDecimal spreadGiveJinBi, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			thisSystemService.executeUpdate(id, orderNoLength, orderOutTime, cashFeeRate, fansWeibiToJinbiRate,
					vipWeibiToJinbiRate, agenterWeibiToJinbiRate, partnerWeibiToJinbiRate, partnerCountsLimit,
					partnerJinbiFromAgentCashRate, spreadGiveWeiBi, spreadGiveJinBi);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}
}
